package com.hl.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.hl.domain.ProductCategory;
import com.hl.mapper.ProductPlatformMapper;
import com.hl.domain.ProductPlatform;
import com.hl.service.IProductPlatformService;

/**
 * 平台商品Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-07
 */
@Service
@Primary
public class ProductPlatformServiceImpl implements IProductPlatformService
{
    @Autowired
    private ProductPlatformMapper productPlatformMapper;

    /**
     * 查询平台商品
     *
     * @param id 平台商品主键
     * @return 平台商品
     */
    @Override
    public ProductPlatform selectProductPlatformById(Integer id)
    {
        return productPlatformMapper.selectProductPlatformById(id);
    }

    /**
     * 查询平台商品列表
     *
     * @param productPlatform 平台商品
     * @return 平台商品
     */
    @Override
    public List<ProductPlatform> selectProductPlatformList(ProductPlatform productPlatform)
    {
        return productPlatformMapper.selectProductPlatformList(productPlatform);
    }

    /**
     * 新增平台商品
     *
     * @param productPlatform 平台商品
     * @return 结果
     */
    @Transactional
    @Override
    public int insertProductPlatform(ProductPlatform productPlatform)
    {
        productPlatform.setCreateTime(DateUtils.getNowDate());
        int rows = productPlatformMapper.insertProductPlatform(productPlatform);
        insertProductCategory(productPlatform);
        return rows;
    }

    /**
     * 修改平台商品
     *
     * @param productPlatform 平台商品
     * @return 结果
     */
    @Transactional
    @Override
    public int updateProductPlatform(ProductPlatform productPlatform)
    {
        productPlatform.setUpdateTime(DateUtils.getNowDate());
        productPlatformMapper.deleteProductCategoryById(productPlatform.getId());
        insertProductCategory(productPlatform);
        return productPlatformMapper.updateProductPlatform(productPlatform);
    }

    /**
     * 批量删除平台商品
     *
     * @param ids 需要删除的平台商品主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProductPlatformByIds(Integer[] ids)
    {
        productPlatformMapper.deleteProductCategoryByIds(ids);
        return productPlatformMapper.deleteProductPlatformByIds(ids);
    }

    /**
     * 删除平台商品信息
     *
     * @param id 平台商品主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProductPlatformById(Integer id)
    {
        productPlatformMapper.deleteProductCategoryById(id);
        return productPlatformMapper.deleteProductPlatformById(id);
    }

    /**
     * 新增商品分类信息
     *
     * @param productPlatform 平台商品对象
     */
    public void insertProductCategory(ProductPlatform productPlatform)
    {
        List<ProductCategory> productCategoryList = productPlatform.getProductCategoryList();
        Integer id = productPlatform.getId();
        if (StringUtils.isNotNull(productCategoryList))
        {
            List<ProductCategory> list = new ArrayList<ProductCategory>();
            for (ProductCategory productCategory : productCategoryList)
            {
                productCategory.setId(id);
                list.add(productCategory);
            }
            if (list.size() > 0)
            {
                productPlatformMapper.batchProductCategory(list);
            }
        }
    }
}
